﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workbench;
using Workbench.Models;
using Workbench.Operations;
using Workbench.Shared;

namespace WorkbenchTests
{
    [TestClass]
    public class TextSegmentationServiceTests
    {
        [TestMethod]
        public void TextSegmentationServiceTests_counts_sentences_with_line_break()
        {
            string text =
@"Tova e stihotvorenie
na Ivan Vazov.
Krasivo, hubavo i gotino."; 
            var result = TextSegmentationService.Process(text);

            Assert.AreEqual(1, result.Paragraphs.Count);

            Assert.AreEqual(2, result.Sentences.Count);

            Assert.AreEqual(10, result.Words.Count);
            Assert.AreEqual("Tova", result.Words[0].GetContent());
            Assert.AreEqual("e", result.Words[1].GetContent());
            Assert.AreEqual("stihotvorenie", result.Words[2].GetContent());
            Assert.AreEqual("na", result.Words[3].GetContent());
            Assert.AreEqual("Ivan", result.Words[4].GetContent());
            Assert.AreEqual("Vazov", result.Words[5].GetContent());
            Assert.AreEqual("Krasivo", result.Words[6].GetContent());
            Assert.AreEqual("hubavo", result.Words[7].GetContent());
            Assert.AreEqual("i", result.Words[8].GetContent());
            Assert.AreEqual("gotino", result.Words[9].GetContent());

            Assert.AreEqual(10, result.DifferentForms);
            Assert.AreEqual(6, result.MaximumNumberOfWordsInSentence);
            Assert.AreEqual(2, result.MaximumNumberOfSentencesInParagraph);
        }
         
        [TestMethod]
        public void TextSegmentationServiceTests_counts_two_paragraphs_correctly()
        {
            string paragraph1 = @"A large open workspace in Sofia, humming with the latest gadgets and a buzz of energy, is home to some of Southeast Europe's brightest entrepreneurial talent. ""The Roof,"" as it is known, is the collaborative office space utilised by 32 business startups funded by the accelerator programme Eleven.";
            string paragraph2 = @"Eleven takes its name from 11 kilometres per second -- the speed that is required for an object to escape the earth's orbit. Dilyan Dimitrov, a co-founder of Eleven, said the venture intends to help entrepreneurs escape the region and achieve global exposure. It is funded under a European Commission initiative that works to improve small- and medium-sized enterprises' access to finance and sustainable growth.";
            string text = TestUtility.JoinParagraphs(paragraph1, paragraph2);

            var result = TextSegmentationService.Process(text);

            Assert.AreEqual(2, result.Paragraphs.Count);
            TestUtility.AssertMultilineStringEqual(paragraph1, result.Paragraphs[0].GetContent(text));
            TestUtility.AssertMultilineStringEqual(paragraph2, result.Paragraphs[1].GetContent(text));
        }

        [TestMethod]
        public void TextSegmentationServiceTests_counts_two_sentences_correctly()
        {
            string sentence1 = @"The quick brown fox jumped over the fence.";
            string sentence2 = @"To be or not to be!";
            string text = string.Join(" ", sentence1, sentence2);

            var result = TextSegmentationService.Process(text);

            Assert.AreEqual(1, result.Paragraphs.Count);
            TestUtility.AssertMultilineStringEqual(text, result.Paragraphs[0].GetContent(text));

            Assert.AreEqual(2, result.Sentences.Count);
            TestUtility.AssertMultilineStringEqual(sentence1, result.Sentences[0].GetContent(text));
            TestUtility.AssertMultilineStringEqual(sentence2, result.Sentences[1].GetContent(text));
        }

        [TestMethod]
        public void TextSegmentationServiceTests_entense_with_parentheses()
        {
            var text = @"The sentence has some text and (other other).";
            var result = TextSegmentationService.Process(text);


            Assert.AreEqual(1, result.Paragraphs.Count);
            TestUtility.AssertMultilineStringEqual(text, result.Paragraphs[0].GetContent(text));
            Assert.AreEqual(1, result.Sentences.Count);
            TestUtility.AssertMultilineStringEqual(text, result.Sentences[0].GetContent(text));
            Assert.AreEqual(8, result.Words.Count);
            Assert.AreEqual("The", result.Words[0].GetContent());
            Assert.AreEqual("sentence", result.Words[1].GetContent());
            Assert.AreEqual("has", result.Words[2].GetContent());
            Assert.AreEqual("some", result.Words[3].GetContent());
            Assert.AreEqual("text", result.Words[4].GetContent());
            Assert.AreEqual("and", result.Words[5].GetContent());
            Assert.AreEqual("other", result.Words[6].GetContent());
            Assert.AreEqual("other", result.Words[7].GetContent());
            Assert.AreEqual(result.Words.Count - 1, result.NewUniqueWords.Count);
        }

        [TestMethod]
        public void TextSegmentationServiceTests_word_with_different_line_endings()
        {
            //there are \r\n and \n mixed here
            var text = @"	[Kodirane UTF-8]

	Нора Робъртс
	Тайни и мечти

";
            var result = TextSegmentationService.Process(text);
            Assert.AreEqual(7, result.Words.Count);
            Assert.AreEqual(2, result.Paragraphs.Count);
            Assert.AreEqual(2, result.Sentences.Count);
        }

        [TestMethod]
        public void TextSegmentationServiceTests_counts_sentences_separated_by_spaces()
        {
            var text = @"— Компания й прави една „GTO“, модел шейсет и седма.
	— С три дявола и четири скорости?
	— Точно.";

            var result = TextSegmentationService.Process(text);
            Assert.AreEqual(3, result.Sentences.Count);
        }

        [TestMethod]
        public void TextSegmentationServiceTests_numbers_and_years_arent_counted()
        {
            //there are \r\n and \n mixed here
            var text = @"На 13/06/2012 имаше 2000 жертви.";
            var result = TextSegmentationService.Process(text);
            
            Assert.AreEqual(3, result.Words.Count);
        }

        [TestMethod]
        public void TextSegmentationServiceTests_tabs_are_stripped_from_sentences()
        {
            var sentence1 = @"Nqkakvo si izrechenie.";
            var sentence2 = @"Nqkakvo si izrechenie.";
            var text = string.Join("\n\t\t", sentence1, sentence2);

            var result = TextSegmentationService.Process(text);

            Assert.AreEqual(2, result.Sentences.Count);
            Assert.AreEqual(sentence1, result.Sentences[0].GetContent(text));
            Assert.AreEqual(sentence2, result.Sentences[1].GetContent(text));
        }

        [TestMethod]
        public void TextSegmentationServiceTests_last_word_before_punct()
        {
            var sentence1 = @"Бързо!";

            var result = TextSegmentationService.Process(sentence1);

            Assert.AreEqual(1, result.Words.Count);
            Assert.AreEqual("Бързо", result.Words[0].UniqueWord.Content);
        }
    }
}
